﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FourPartHarmony3 {
    class StaffStatus {
        private List<LN> keySharpList = new List<LN>();
        private List<LN> keyFlatList = new List<LN>();

        private List<LN> accidentalFlatList = new List<LN>();
        private List<LN> accidentalSharpList = new List<LN>();
        private List<LN> accidentalNaturalList = new List<LN>();

        // 同時刻に同じ音が2つあって臨時記号がついているとき両方につける必要があるので、ここに一旦貯めておく。
        private List<LN> accidentalFlatToAdd = new List<LN>();
        private List<LN> accidentalSharpToAdd = new List<LN>();
        private List<LN> accidentalNaturalToAdd = new List<LN>();

        public MusicKeyInfo Key { get; set; }
        public MeterStruct Meter { get; set; }

        public bool ShouldDrawClef { get; set; }

        public bool ShouldDrawMeter { get; set; }

        public void AddAccidentalFlat(LN ln) {
            accidentalFlatToAdd.Add(ln);
        }

        public void AddAccidentalSharp(LN ln) {
            accidentalSharpToAdd.Add(ln);
        }

        public void AddAccidentalNatural(LN ln) {
            accidentalNaturalToAdd.Add(ln);
        }

        public bool IsLnSharp(LN ln) {
            if (keySharpList.Contains(ln)) {
                return true;
            }
            return accidentalSharpList.Contains(ln);
        }

        public bool IsLnFlat(LN ln) {
            if (keyFlatList.Contains(ln)) {
                return true;
            }
            return accidentalFlatList.Contains(ln);
        }
        
        public bool IsLnNatural(LN ln) {
            return accidentalNaturalList.Contains(ln);
        }

        public void CommitAccidentals() {
            accidentalFlatList.AddRange(accidentalFlatToAdd);
            accidentalSharpList.AddRange(accidentalSharpToAdd);
            accidentalNaturalList.AddRange(accidentalNaturalToAdd);
            accidentalFlatToAdd.Clear();
            accidentalSharpToAdd.Clear();
            accidentalNaturalToAdd.Clear();
        }

        public void SetKeySharpFlat(List<LN> sharpList, List<LN> flatList) {
            keySharpList = sharpList;
            keyFlatList = flatList;
        }

        public void ResetAccidentals() {
            accidentalFlatList.Clear();
            accidentalSharpList.Clear();
            accidentalNaturalList.Clear();
            accidentalFlatToAdd.Clear();
            accidentalSharpToAdd.Clear();
            accidentalNaturalToAdd.Clear();
        }

        public Vector Pos { get; set; }

        public StaffStatus(Vector pos) {
            Pos = pos;
            ShouldDrawClef = true;
        }

        public void AdvanceX(double offsetX) {
            Pos = new Vector(Pos.X + offsetX, Pos.Y);
        }
    }
}
